package com.zmn.oms.business.impl.messageV1.baidu;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.common.constant.MessageControlSwitchConsts;
import com.zmn.oms.business.interfaces.messageV1.common.MessageSendBaseService;
import com.zmn.oms.business.interfaces.messageV1.baidu.MessageSendBaiduService;
import com.zmn.oms.common.constant.MessageRuleIdConsts;
import com.zmn.oms.model.dto.messageV1.BaiduMessageDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * 类描述：百度消息发送
 *
 * @author yule
 * @date 2020/12/23 14:22
 */
@Service
@Slf4j
public class MessageSendBaiduServiceImpl extends AbstractMessageSendBaidu implements MessageSendBaiduService {

    @Autowired
    private MessageSendBaseService messageSendBaseService;

    @Override
    public boolean sendBaiduMessage(OrderWork orderWork, OrderDetail orderDetail, Integer ruleId) {
        logger.info("百度消息发送开始 ---------------------------------------------");

        // 分单未登入消息转分单普通消息规则
        if (Objects.equals(MessageRuleIdConsts.ZMN_MSG_RULEID_ASSIGN_NO_LOGIN, ruleId)) {
            ruleId = MessageRuleIdConsts.ZMN_MSG_RULEID_ASSIGN;
        }

        // 百度消息开关，发送百度消息必须有场景sceneId，如需更改开关，请先确保规则下是否有场景id
        boolean isAbleSendMessage = MessageControlSwitchConsts.baiDuAbleSendRuled.contains(ruleId);
        if (!isAbleSendMessage) {
            logger.info("工单号[{}]的ruleId不合法或不发送百度消息===========", orderWork.getOrderId());
            return false;
        }

        // 场景id
        Map<Integer, String> sceneIds = listSceneIds(orderWork.getOrderId());
        if (sceneIds == null) {
            logger.info("---未找到工单号[{}]的场景sceneId不处理", orderWork.getOrderId());
            return false;
        }
        logger.info("订单号【{}】获取场景id集合 {}", orderWork.getOrderId(), JSON.toJSON(sceneIds));

        // 获取百度OpenId
        String openId = getBaiduUserOpenId(orderWork.getOrderId(), orderWork.getUserId(), orderWork.getChannelId());
        if (StringUtils.isBlank(openId)) {
            logger.info("未找到工单号[{}]用户userId[{}]的百度的openId", orderWork.getOrderId(), orderWork.getUserId());
            return false;
        }

        // 获取场景ID
        String sceneId = sceneIds.get(ruleId);
        if (StringUtils.isBlank(sceneId)) {
            logger.info("未找到工单号[{}]的ruleId={}的场景Id ", orderWork.getOrderId(), ruleId);
            return false;
        }
        // 获取消息参数
        Map<String, String> smsMap = handleBaiduParams(orderWork, orderDetail, ruleId);
        BaiduMessageDTO baiduMessageDTO = BaiduMessageDTO.builder()
                .openId(openId)
                .ruleId(ruleId)
                .sceneId(sceneId)
                .objectId(String.valueOf(orderWork.getUserId()))
                .orderId(orderWork.getOrderId())
                .params(JSON.toJSONString(smsMap))
                .channelId(String.valueOf(orderWork.getChannelId()))
                .cityId(String.valueOf(orderWork.getCityId()))
                .page(null)
                .ext(null)
                .build();
        return messageSendBaseService.sendBaiduMessage(baiduMessageDTO);
    }
}
